import {objDeepCopy} from "@/assets/js/common.js";
let Controller = {
	mounted() {
		this.getTeamName();
	},
	methods: {
		//获取分组名称
		getTeamName() {
			//获取所有系列
			this.$ajax({
				method: 'post',
				url:this.apiTest+'/sale/saleGoodsGroup/list',
			}).then((res)=>{
				var data = res.data;		
				if(data.status == 200){
					var result = data.result;
					this.Model.filterOption3.items[2].data = result.map(item => {
						return {
							label: item.name,
							value: item.id
						}
					});
					for (var i = 0; i < result.length; i++) {
						this.$set(result[i], 'isEdit', false)
						this.$set(result[i], 'active', false)
					}
					this.Model.TeamName = objDeepCopy(result);
					console.log(this.Model.TeamName)
				}else{
					this.$message.error(data.msg);
				}
			});
		},
		//获取列表
		getAllList(params) {
			this.$ajax({
				method: 'post',
				url:this.apiTest+'/sale/saleGoodsGroupDetail/list',
				data:params
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
					var result = data.result.result;
					console.log(result)
					this.Model.dialogTableData  = result;
					//分页
					this.Model.pager.params.pageSize = data.pageSize;
					this.Model.pager.params.pageIndex = data.pageIndex;
					this.Model.pager.pageTotal = data.pageTotal;
				}else{
					this.$message.error(data.msg);
				}
			});
		},
		showNotSureBarCodeP2(val){
			this.Model.searchFromData2 = val;
		},
		toSearchSureBarCode(val) {
			console.log(this.Model.searchFromData)
		},
		//搜索商品
		toSearchNotSureBarCode(val) {
			//console.log(this.Model.searchFromData2.saleGoodsGroupId)
			var params ={
				condition:{
					saleGoodsGroupId:this.Model.searchFromData2.saleGoodsGroupId
				}
			}
			this.getAllList(params);
		},
		toClear(val) {
			this.Model.searchFromData3 = this.Model.defaultSearchFromData3;
		},
		handleSelectionChange(val) {
			console.log(val);
		},
		handleCurrentChange(val) {
			this.Model.pager.params.pageIndex = val;
		},
		handleSizeChange(val) {
			this.Model.pager.params.pageSize = val;
		},
		addChooseShop() {
			this.$router.push({name:'priceCheck'})
		},	
		removeChooseShop() {
			this.Model.removeChooseShop = status;
		},	
		//确认加入分组
		addSure(val) {
			var mainTableData = this.Model.TableSelectData;
			var arr = [];
			for (var i = 0; i < mainTableData.length; i++) {	
				if(mainTableData[i].barCode){
					var obj = {
						saleGoodsGroupId:this.Model.chooseTeam,
						saleGoodsInfoId:mainTableData[i].id,
						isFinishProduct:0,
					}
				}else{
					var obj = {
						saleGoodsGroupId:this.Model.chooseTeam,
						saleGoodsInfoId:mainTableData[i].id,
						isFinishProduct:1
					}
				}
				if(mainTableData[i].saleGoodsMasterId){
					obj.saleGoodsMasterId = mainTableData[i].aSeriesOf
				}
				arr.push(obj);
			}
			this.$ajax({
				method: 'post',
				//url:'/sale/saleGoodsGroupDetail/addGoods',
				url:this.apiTest+'/sale/saleGoodsGroupDetail/addGoods',
				data:arr
			}).then((res)=>{
				var data = res.data;		
				if(data.status == 200){
					var result = data.result;
					console.log(result);
					this.$message({
						type: 'success',
						message: '已加入成功'
					});
					this.Model.addProduct = false;
				}else{
					this.$message.error(data.msg);
				}
			});
		},
		//修改组名
		SaveWrite (item) {
			item.isEdit = false;
			console.log(item.id)
			this.$ajax({
				method: 'put',
				//url:'/sale/saleGoodsGroupDetail/addGoods',
				url:this.apiTest+'/sale/saleGoodsGroup/update',
				data:{
					id:item.id,
					name:item.name
				}
			}).then((res)=>{
				var data = res.data;		
				if(data.status == 200){
					this.$message({
						type: 'success',
						message: '已修改成功'
					});
					//this.Model.TeamName = objDeepCopy(result);
				}else{
					this.$message.error(data.msg);
				}
			});
			
		},
		//新增分组确认
		addSureteam() {
			let teamName = this.Model.Groupingname;
			if(teamName.length >= 10 ){
				this.$message.error('超出10个字符');
			}else{
				this.$ajax({
					method: 'post',
					//url:'/sale/saleGoodsGroupDetail/addGoods',
					url:this.apiTest+'/sale/saleGoodsGroup/add',
					data:{
						name:teamName
					}
				}).then((res)=>{
					var data = res.data;		
					if(data.status == 200){
						this.$message({
							type: 'success',
							message: '已新增成功'
						});
						this.getTeamName();
						this.Model.addTeam = false;
					}else{
						this.$message.error(data.msg);
					}
				})
			}
		},
		//分组名称编辑
		Towrite(item,index) {
			// 非编辑状态下切换为编辑状态
			console.log(index)
			if (!item.isEdit) {
				item.isEdit = true;
				//聚焦输入框
				this.$nextTick(() => {
					this.$refs.teamInput[index].$el.querySelector('input').focus()
				});
			} else {
				// 编辑状态下保存，将tempLabel的值付给label，退出编辑状态
		
				item.isEdit = false
			}
		},
		//删除分组
		del (item,index) {
			this.Model.TeamName.splice(index, 1);
			let groupID = item.id;
			this.$ajax({
				method: 'DELETE',
				//url:'/sale/saleGoodsGroupDetail/addGoods',
				url:this.apiTest+'/sale/saleGoodsGroup/'+groupID,
			}).then((res)=>{
				var data = res.data;		
				if(data.status == 200){
					this.$message({
						type: 'success',
						message: '已删除成功'
					});
					//this.Model.TeamName = objDeepCopy(result);
				}else{
					this.$message.error(data.msg);
				}
			});
		},
		handleSelectionChange(val) {
			this.Model.TableSelectData = val;
		},
		addTeam() {
			this.Model.addTeam = status;
		},	
		joinTeam() {
			this.Model.addProduct = status;
		},	
	}	
}
export default Controller;
